package com.gzsxy.esjy.request;


import com.gzsxy.esjy.SvConstant;
import com.gzsxy.esjy.common.Assert;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import org.springframework.util.StringUtils;

import java.nio.charset.Charset;

/**
 * 请求信息
 * @author zhangxiaolong
 * @date 2022/7/6 9:36
 */
public class RequestMessage {

	private HttpMethod method = HttpMethod.GET ;
	
	private ContentType contentType =ContentType.APPLICATION_JSON ;
	
	private HttpHeaders headers ;
	
	private String charset="utf-8" ;
	
	private long contentLength ;
	
	public long getContentLength() {
		return contentLength;
	}
	
	private RequestMessage(HttpHeaders headers, HttpMethod method, String contentType, String charset, long length) {
		super();
		this.method = method;
		this.headers = headers ;
		this.contentLength = length ;
		if(StringUtils.hasText(contentType)) {
			this.contentType = ContentType.valueOf(contentType);
		}
		if(StringUtils.hasText(charset) && Charset.isSupported(charset)) {
			this.charset = charset;
		}
	}

	/**
	 * 创建请求信息
	 * @param request
	 * @return
	 */
	public static RequestMessage createRequestMessage(HttpRequest request) {
		//获取请求头
		String contentType = request.headers().get(HttpHeaderNames.CONTENT_TYPE);
		//获取数据长度
		long length = request.headers().getInt(HttpHeaderNames.CONTENT_LENGTH, -1) ;
		String[] splitContypeType = splitContypeType(contentType);
		return new RequestMessage(request.headers(),request.method(), splitContypeType[0], splitContypeType[1],length);
		
	}
	
	/**
	 * 获取contentType
	 * @param contentType
	 * @return
	 */
	private static String[] splitContypeType(String contentType) {
		//为空
		if(!StringUtils.hasText(contentType)) {
			return new String[] {null,null} ;
		}
		//content-type 为 multipart/form-data; boundary=----WebKitFormBoundaryy1rBvHvbc0IdjF9h  上传文件
		int index = contentType.indexOf(";");
		//不为上传文件
		if(index < 0) {
			return new String[] {contentType,null} ;
		}
		//multipart/form-data
		String contentTypeV = contentType.substring(0, contentType.indexOf(";"));
		
		return new String[] {contentTypeV,getCharsetParam(contentType.substring(index+1).toLowerCase())} ;
	}
	
	/**
	 * 获取编码
	 * @param lastString  boundary=----WebKitFormBoundaryy1rBvHvbc0IdjF9h
	 * @return
	 */
	private static String getCharsetParam(String lastString) {
		String[] split = SvConstant.SPLIT_PATTERN_2.split(lastString) ;
		for (String keyValue : split) {
			String[] vlaues = SvConstant.SPLIT_PATTERN_3.split(keyValue) ;
			Assert.notTrue(vlaues.length == 2, lastString+" is Illegal ",400);
			//获取编码
			if("charset".equals(vlaues[0])) {
				return vlaues[1];
			}
		}
		return null;
	}
	
	

	public HttpMethod getMethod() {
		return method;
	}

	public ContentType getContentType() {
		return contentType;
	}

	public String getCharset() {
		return charset;
	}

	public HttpHeaders getHeaders() {
		return headers;
	}

	public void setHeaders(HttpHeaders headers) {
		this.headers = headers;
	}

}
